package com.ruoyi.fztz.tbzn.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.fztz.tbzn.domain.Tbzn;
import com.ruoyi.fztz.tbzn.domain.TbznFj;
import com.ruoyi.fztz.tbzn.mapper.TbznMapper;
import com.ruoyi.fztz.tbzn.service.ITbznService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 填报指南Service业务层处理
 *
 * @author huacai
 * @date 2024-11-29
 */
@Service
public class TbznServiceImpl implements ITbznService
{
    @Autowired
    private TbznMapper tbznMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询填报指南
     *
     * @param tbznId 填报指南主键
     * @return 填报指南
     */
    @Override
    public Tbzn selectTbznByTbznId(String tbznId)
    {
        return tbznMapper.selectTbznByTbznId(tbznId);
    }

    /**
     * 查询填报指南列表
     *
     * @param tbzn 填报指南
     * @return 填报指南
     */
    @Override
    public List<Tbzn> selectTbznList(Tbzn tbzn)
    {
        return tbznMapper.selectTbznList(tbzn);
    }

    /**
     * 新增填报指南
     *
     * @param tbzn 填报指南
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbzn(Tbzn tbzn)
    {
        tbzn.setIdWhere("存在");
        tbzn.setCreateTime(DateUtils.getNowDate());
        int rows = tbznMapper.insertTbzn(tbzn);
        insertTbznFj(tbzn);
        return rows;
    }

    /**
     * 批量新增填报指南
     *
     * @param tbzns 填报指南List
     * @return 结果
     */
    @Override
    public int batchInsertTbzn(List<Tbzn> tbzns)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(tbzns)) {
            try {
                for (int i = 0; i < tbzns.size(); i++) {
                    int row = tbznMapper.insertTbzn(tbzns.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == tbzns.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改填报指南
     *
     * @param tbzn 填报指南
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbzn(Tbzn tbzn)
    {
        tbzn.setUpdateTime(DateUtils.getNowDate());
//        tbznMapper.deleteTbznFjByTbznId(tbzn.getTbznId());
        insertTbznFj(tbzn);
        return tbznMapper.updateTbzn(tbzn);
    }

    /**
     * 批量删除填报指南
     *
     * @param tbznIds 需要删除的填报指南主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbznByTbznIds(String[] tbznIds)
    {
        tbznMapper.deleteTbznFjByTbznIds(tbznIds);
        return tbznMapper.deleteTbznByTbznIds(tbznIds);
    }

    /**
     * 删除填报指南信息
     *
     * @param tbznId 填报指南主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbznByTbznId(String tbznId)
    {
        tbznMapper.deleteTbznFjByTbznId(tbznId);
        return tbznMapper.deleteTbznByTbznId(tbznId);
    }

    /**
     * 新增附件
     */
    @Override
    public int insertTbznFjByfztzId(TbznFj tbznFj) {
        return tbznMapper.insertTbznFjByfztzId(tbznFj);
    }

    /**
     * 根据ID查询附件列表
     */
    @Override
    public List<TbznFj> selectTbznFjList(TbznFj tbznFj) {
        return tbznMapper.selectTbznFjList(tbznFj);
    }
    /**
     * 删除填报指南附件
     */
    @Override
    public int deleteTbznFjByFjIds(Integer[] fjIds) {
        return tbznMapper.deleteTbznFjByFjIds(fjIds);
    }

    /**
     * 新增填报指南附件信息
     *
     * @param tbzn 填报指南对象
     */
    public void insertTbznFj(Tbzn tbzn)
    {
        List<TbznFj> tbznFjList = tbzn.getTbznFjList();
        String tbznId = tbzn.getTbznId();
        if (StringUtils.isNotNull(tbznFjList))
        {
            List<TbznFj> list = new ArrayList<TbznFj>();
            for (TbznFj tbznFj : tbznFjList)
            {
                tbznFj.setTbznId(tbznId);
                list.add(tbznFj);
            }
//            if (list.size() > 0)
//            {
//                tbznMapper.batchTbznFj(list);
//            }
        }
    }
}
